package com.wj.facedetect.util;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import static com.wj.facedetect.util.TimeUtils.DATE_FORMAT_YYYYMMDDHHMMSS1;

/**
 * Created by wujing on 2017/5/8.
 */

public class CrashHandlerUtil implements Thread.UncaughtExceptionHandler {
    private static final String TAG = "CrashHandlerUtil";

    private static final String CRASH_PATH;

    static {
        CRASH_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + "faceddlog" + File.separator
                + "crash" + File.separator;
    }

    private Thread.UncaughtExceptionHandler mDefaultHandler;

    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {
        handleException(throwable);

        if (mDefaultHandler != null) {
            mDefaultHandler.uncaughtException(thread, throwable);
        }
    }

    private void handleException(final Throwable throwable) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                if (throwable == null) {
                    return;
                }
                String fileName = TimeUtils.getFormatTimeByType(System.currentTimeMillis(), DATE_FORMAT_YYYYMMDDHHMMSS1) + ".txt";
                File dir = new File(CRASH_PATH);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File crashFile = new File(dir, fileName);
                PrintWriter writer = null;
                try {
                    writer = new PrintWriter(new FileOutputStream(crashFile));
                    throwable.printStackTrace(writer);
                    writer.flush();
                } catch (Exception e) {
                    Log.e(TAG, "handleException: error", e);
                } finally {
                    writer.close();
                }
            }
        });
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.start();
    }

    public void handleLog(final String string) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                if (string == null) {
                    return;
                }
                String fileName = "TcpService" + ".txt";
                File dir = new File(CRASH_PATH);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File crashFile = new File(dir, fileName);
                if (crashFile == null) {
                    return;
                }
                PrintWriter writer = null;
                try {
                    writer = new PrintWriter(new FileOutputStream(crashFile));
                    writer.write(string);
                    writer.flush();
                } catch (Exception e) {
                    Log.e(TAG, "handleException: error", e);
                } finally {
                    writer.close();
                }
            }
        });
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.start();
    }

    /**
     * 保证只有一个CrashHandler实例
     */
    private CrashHandlerUtil() {
    }

    private static class InstanceHolder {
        /**
         * CrashHandler实例
         */
        private static final CrashHandlerUtil INSTANCE = new CrashHandlerUtil();
    }

    /**
     * 获取CrashHandler实例 ,单例模式
     */
    public static CrashHandlerUtil getInstance() {
        return InstanceHolder.INSTANCE;
    }

    public void init() {
        // 获取系统默认的UncaughtException处理器
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        // 设置该CrashHandler为程序的默认处理器
        Thread.setDefaultUncaughtExceptionHandler(this);
    }
}
